/*
 * $Id$
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

function clearErrorMessagesXHTML(form) {

    // Clear existing errors on submit
	$(form).find("div.has-error").removeClass("has-error");
	$(form).find("div.has-feedback").removeClass("has-feedback");
	$(form).find("span.s2_help_inline").remove();
	$(form).find("span.s2_feedback").remove();
	$(form).find("div.s2_validation_errors").remove();
}

function clearErrorMessages(form) {
    clearErrorMessagesXHTML(form);
}

function clearErrorLabelsXHTML(form) {
    // set all labels back to the normal class
    /*var i, elements = form.elements;
    for (i = 0; i < elements.length; i++) {

        var parentEl = elements[i];
        // search for the parent table row, abort if the form is reached
        // the form may contain "non-wrapped" inputs inserted by Dojo
        while (parentEl.nodeName.toUpperCase() !== "TR" && parentEl.nodeName.toUpperCase() !== "FORM") {
            parentEl = parentEl.parentNode;
        }
        if (parentEl.nodeName.toUpperCase() === "FORM") {
            parentEl = null;
        }

         //if labelposition is 'top' the label is on the row above
        if(parentEl && parentEl.cells) {
          var labelRow = parentEl.cells.length > 1 ? parentEl : StrutsUtils.previousElement(parentEl, "tr");
          if (labelRow) {
              var cells = labelRow.cells;
              if (cells && cells.length >= 1) {
                  var label = cells[0].getElementsByTagName("label")[0];
                  if (label) {
                      label.setAttribute("class", "label");
                      label.setAttribute("className", "label"); //ie hack cause ie does not support setAttribute
                  }
              }
          }
        }
    }*/

}

function clearErrorLabels(form) {
    clearErrorLabelsXHTML(form);
}

function addErrorXHTML(e, errorText) {
    try {
	    //Handle field errors
	    if (e.type = 'input') {
			var element = $(e), controlGroup, controls;

            controlGroup = element.closest("div.form-group");
            controlGroup.addClass('has-error');
            controlGroup.addClass('has-feedback');
            controls = controlGroup.find("div.controls");
            if (controls) {
                if(!(element.is(':radio') || element.is(':checkbox'))) {
					if(controls.find('span.form-control-feedback.s2_feedback').length == 0)
                    	controls.append("<span class='glyphicon glyphicon-remove form-control-feedback s2_feedback'></span>");
                }
                controls.append("<span class='help-block s2_help_inline'>" + errorText + "</span>");
            }
	    } else {        
			//Handle non field errors
	        var errorDiv = $("<div class='alert alert-danger s2_validation_errors'></div>");
	        $(form).prepend(errorDiv);
            errorDiv.append('<p>' + errorText + '</p>\n');
		}
    } catch (err) {
        alert(err);
    }
}

function addError(e, errorText) {
    addErrorXHTML(e, errorText);
}

